package com.mall.common.domain;

import static com.mall.common.bean.CommonAttributes.PATH_SEPARATOR;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name="s_area")
@JsonIgnoreProperties(ignoreUnknown=true) 
@XmlRootElement
public class AreaDomain extends PriorityEntity {

	private static final long serialVersionUID = -2158109459123036967L;

	private String name;// 地区名称
	private String path;// 树路径
	private String full_name; //全路径
	private String serialNumber; //行政编号
	@JsonIgnore private AreaDomain parent;// 上级地区
	@JsonIgnore private Set<AreaDomain> children;// 下级地区
	
	public AreaDomain(String name, String path, String full_name,
			AreaDomain parent, Set<AreaDomain> children) {
		super();
		this.name = name;
		this.path = path;
		this.full_name = full_name;
		this.parent = parent;
		this.children = children;
	}
	
	public AreaDomain() {
		super();
	}

	@Transient
	public String getFullPath(){
		return path + getId();
	}

	//获取去掉首尾逗号的path
	@Transient
	public String getAreaPath(){
		String areaPath = path;
		if(areaPath == null){
			return "";
		}
		if(areaPath.indexOf(",") == 0){
			areaPath = areaPath.substring(1, areaPath.length());
		}
		if(areaPath.lastIndexOf(",") >= 0 
				&&areaPath.lastIndexOf(",") == areaPath.length() - 1){
			areaPath = areaPath.substring(0, areaPath.length() - 1);
		}
		return areaPath;
	}
	
	@Column(nullable = false)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getFull_name() {
		return full_name;
	}

	public void setFull_name(String full_name) {
		this.full_name = full_name;
	}

	public String getSerialNumber() {
		return serialNumber;
	}

	public void setSerialNumber(String serialNumber) {
		this.serialNumber = serialNumber;
	}

	@Column(nullable = true, length = 10000)
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="parent_id")
	public AreaDomain getParent() {
		return parent;
	}

	public void setParent(AreaDomain parent) {
		this.parent = parent;
	}

	@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
	@Cascade(value = { CascadeType.DELETE })
	@OrderBy("priority asc")
	public Set<AreaDomain> getChildren() {
		return children;
	}

	public void setChildren(Set<AreaDomain> children) {
		this.children = children;
	}

	// 获取分类层级（顶级分类：0）
	@Transient
	public Integer getLevel() {
		return path.split(PATH_SEPARATOR).length - 1;
	}
}